# 获取主分类
from flask import request, Blueprint

from api.decorators import cache
from api.slider import get_sort_major_info, get_json_response, get_sort_all_info, get_sort_book

book_sort = Blueprint('book_sort', __name__)


@book_sort.route('/sort')
@cache
def sort_major_info_response():
    content = get_sort_major_info()
    del content['picture']
    return get_json_response(content, request)


# 获取主分类和子分类
@book_sort.route('/sort/all')
@cache
def sort_all_response():
    content = get_sort_all_info()
    return get_json_response(content, request)


# 根据分类获取书籍列表
@book_sort.route('/sort/book')
@cache
def sort_book_response():
    """
    gender: 男生:male 女生:female 出版:press
    type: 热门:hot 新书:new 好评:repulation 完结: over
    major: 大类别 从接口1获取
    minor: 小类别 从接口4获取 (非必填)
    start: 分页开始页
    limit: 分页条数
    :return:
    """
    gender = request.args.get('gender', 'male')
    boo_type = request.args.get('type', 'hot')
    major = request.args.get('major', '奇幻')
    minor = request.args.get('minor', '')
    start = request.args.get('start', 1, int) - 1
    limit = request.args.get('limit', 10, int)
    start = start * limit
    query = {
        'gender': gender,
        'type': boo_type,
        'major': major,
        'minor': minor,
        'start': start,
        'limit': limit,
    }
    content = get_sort_book(query)
    return get_json_response(content, request)
